package top.eggcode.base;

import org.apache.commons.lang3.StringUtils;
import top.eggcode.common.model.Identifier;
import top.eggcode.common.model.UidGenerator;
import top.eggcode.security.shiro.SecurityUtil;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Transient;
import java.time.LocalDateTime;

/**
 * 通用属性实体
 * 据实际的实际情况设置属性
 * Created at 2021/3/20 14:33
 *
 * @author DingJiaQi
 * @version 1.0
 */
@MappedSuperclass
public abstract class BaseEntity implements Identifier<String> {

    /**
     * 保证序列化兼容性
     */
    @Transient
    private static final long serialVersionUID = 1L;

    /**
     * 唯一标识
     */
    @Id
    private String id;

    /**
     * 删除的
     */
    @Column(columnDefinition = "TINYINT")
    private Boolean deleted = false;

    /**
     * 创建时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime createdAt = LocalDateTime.now();

    /**
     * 修改时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime updatedAt = LocalDateTime.now();

    /**
     * 删除时间
     */
    @Column(columnDefinition = "timestamp")
    private LocalDateTime deletedAt = LocalDateTime.now();

    /**
     * 创建人
     */
    private String createdBy = "";

    /**
     * 修改人
     */
    private String updatedBy = "";

    /**
     * 删除人
     */
    private String deletedBy = "";

    /**
     * 初始化主键
     *
     * @return 主键
     */
    public String initId() {
        this.id = UidGenerator.apply();
        return this.getId();
    }

    /**
     * 创建痕迹
     */
    public void markCreated() {
        if (StringUtils.isBlank(this.getId())) {
            this.initId();
        }
        // 未删除
        this.deleted = false;

        this.createdAt = LocalDateTime.now();

        String username = SecurityUtil.getUsername();

        if (StringUtils.isBlank(username)) {
            username = "#system";
        }
        // 当前用户
        this.createdBy = username;
    }

    /**
     * 更新痕迹
     */
    public void markUpdated() {

        this.updatedAt = LocalDateTime.now();

        String username = SecurityUtil.getUsername();

        if (StringUtils.isBlank(username)) {
            username = "#system";
        }
        // 当前用户
        this.updatedBy = username;
    }

    /**
     * 逻辑删除
     */
    public void markDeleted() {
        // 标记删除
        this.deleted = true;
        this.deletedAt = LocalDateTime.now();
        String username = SecurityUtil.getUsername();

        if (StringUtils.isBlank(username)) {
            username = "#system";
        }
        this.deletedBy = username;
    }

    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Boolean getDeleted() {
        return deleted;
    }

    public void setDeleted(Boolean deleted) {
        this.deleted = deleted;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public LocalDateTime getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(LocalDateTime deletedAt) {
        this.deletedAt = deletedAt;
    }

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public String getUpdatedBy() {
        return updatedBy;
    }

    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }

    public String getDeletedBy() {
        return deletedBy;
    }

    public void setDeletedBy(String deletedBy) {
        this.deletedBy = deletedBy;
    }
}
